package applications.alternativa3d {
	import flash.geom.Vector3D;
	import jazztures.utils.GeometryUtils;
	import alternativa.engine3d.animation.AnimationController;
	import alternativa.Alternativa3D;
	import alternativa.engine3d.alternativa3d;
	import display3d.alternativa.Floor;
	import alternativa.engine3d.materials.FillMaterial;
	import alternativa.engine3d.primitives.GeoSphere;
	import alternativa.engine3d.core.View;
	import alternativa.engine3d.containers.ConflictContainer;
	import alternativa.engine3d.core.Camera3D;
	import display3d.alternativa.Focus;
	import ui.KeyboardController;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.ui.Keyboard;
	import ui.KinectWheelController;



	[SWF(width="1000", height="800", frameRate="60", backgroundColor="#000000")]
	public class CarDrivingTest extends Sprite
	{	
		private var world : ConflictContainer;
		private var camera : Camera3D;
		private var focus: Focus;
		private var floor:Floor;
		
		private var keyboardController : KeyboardController;
		private var kinectController : KinectWheelController;
		
		
//		private var floor : Floor;

		public function CarDrivingTest()
		{
			super();
			
			
			world = new ConflictContainer();
				
			camera = new Camera3D();
			camera.view = new View(stage.stageWidth, stage.stageHeight);
			camera.y = -500;
			camera.z = -1000;
			camera.fov = Math.PI/4;
			world.addChild(camera);
			addChild(camera.view);
			addChild(camera.diagram);
			
			focus = new Focus();
			focus.y = -200;
			world.addChild(focus);
			
			floor = new Floor();
			world.addChild(floor);
			
			kinectController = new KinectWheelController();
			
			addEventListener(Event.ADDED_TO_STAGE, addedToStageListener);
		}

		private function addedToStageListener(event : Event) : void {
			removeEventListener(Event.ADDED_TO_STAGE, addedToStageListener);
			
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			
			stage.addEventListener(Event.RESIZE, onStageResize);
			
			keyboardController = new KeyboardController(stage);
			
			addEventListener(Event.ENTER_FRAME, handleEnterFrame);
//			addEventListener(Event.ENTER_FRAME, keyboardControllerListener);
			addEventListener(Event.ENTER_FRAME, kinectControllerListener);
		}
		
		

		private function keyboardControllerListener(event : Event) : void {
			if(keyboardController.isDown(Keyboard.UP)) focus.accelerate(); else focus.decelerate();
			
			if(keyboardController.isDown(Keyboard.LEFT)) focus.turn(-1); 
				else if(keyboardController.isDown(Keyboard.RIGHT)) focus.turn(1); 
				else focus.turn(0);
			
			if(keyboardController.isDown(Keyboard.DOWN)) focus.brake();
		}
		
		private function kinectControllerListener(event : Event) : void {
			if(kinectController.acceleration>0) focus.accelerate(kinectController.acceleration); else focus.decelerate();
			
			focus.turn(kinectController.wheelRotation); 
			
			if(kinectController.isBraking) focus.brake();
		}
		

		private function onStageResize(event : Event) : void
		{
			camera.view.width = stage.stageWidth;
			camera.view.height = stage.stageHeight;
		}

		private function handleEnterFrame(ev : Event) : void {
			camera.rotationY = GeometryUtils.getYaw(new Vector3D(camera.x, camera.y, camera.z), new Vector3D(focus.x, focus.y, focus.z),false);
			camera.rotationX = GeometryUtils.getPitch(new Vector3D(camera.x, camera.y, camera.z), new Vector3D(focus.x, focus.y, focus.z),false, "x", "y", "z");
			focus.update();
			camera.render();
		}
	}
}